<?php


namespace app\common\service;


use app\model\SysConfig;

class ConfigService
{

    static function getConfig($key)
    {
        $data = SysConfig::where("key", '=', $key)
            ->find();
        return json_decode($data['value'] ?? "[]", true) ?? [];
    }

    static function getData($key)
    {
        $data = SysConfig::where("key", '=', $key)
            ->find();
        $data['value'] = json_decode($data['value'], true) ?? [];
        return $data;
    }

    static function setConfig($key, $value, $remark = "")
    {
        $ex = SysConfig::where("key", '=', $key)
                ->count() > 0;
        if ($ex) {
            SysConfig::where("key", '=', $key)
                ->update([
                    'value' => json_encode($value),
                    'remark' => $remark,
                ]);
        } else {
            SysConfig::where("key", '=', $key)
                ->insert([
                    'key' => $key,
                    'value' => json_encode($value),
                    'remark' => $remark,
                ]);
        }

    }
}